#!/bin/sh

#Copyright (c) 2014 Luigi Tarenga <luigi.tarenga@gmail.com>
#Distributed under the terms of a MIT license.

# parameters: [ lock_name ]

export LANG=C

cd ${0%/*}

lname=${1:-default}

read lock_servers < conf/lock_servers
read rpath        < conf/remote_path
read sshopt       < conf/sshopt

if [ -f temp/${lname}_counter ] ; then
   read counter  < temp/${lname}_counter
   read holder term value previous < temp/${lname}_htvp

   echo local term values for $lname:
   echo holder:  $holder
   echo term:    $term
   echo value:   $value
   echo counter: $counter
   echo previous: $previous
else
   echo lock $lname not initialized
fi

IFS=: read hostname majority control_master < temp/${lname}_params

echo
echo remote state values for $lname:
echo lock_server holder term value
for h in $lock_servers ; do
   echo $h: $(ssh $sshopt $h "cd $rpath ; " \
                             "[ -f state/$lname ] && " \
                             "cat state/$lname || " \
                             "echo lock file not found")
done
echo
